var constract = require('../../../utils/constract.js');
var app = getApp();

// pages/input-card/mark/mark.js
Page({

  data: {
    remark_list: [],
    state: 0, //区分名片和名名用户
  },

  onLoad: function (options) {
    var that = this;
    var cardId = options.id;
    var state = options.state;
    var name = options.name;
    this.setData({
      cardId: cardId,
      state: state,
      name: name
    });
  },

  onShow: function(){
    var that = this;
    var cardId = this.data.cardId;
    var state = this.data.state;
    app.Verification(function (token) {
      wx.request({
        url: constract.remarkList,
        header: {token: token},
        data: { cardId: cardId, state: state },
        success: function (res) {
          console.log(res);
          that.setData({
            remark_list: res.data.data
          })
        }
      })
    })
  },

  addMark: function() {
    var that = this;
    var cardId = that.data.cardId;
    var name = that.data.name;
    var state = that.data.state;
    
    wx.showActionSheet({
      itemList: ['普通备注','拜访日志'],
      success: function(res) {
        if(res.tapIndex == 0) {
          wx.navigateTo({
            url: '../add-mark/add-mark?cardId=' + cardId + '&markType=2&state='+state,
          })
        } else {
          wx.navigateTo({
            url: '../add-mark/add-mark?cardId=' + cardId +'&markType=1&name='+name+'&state='+state,
          })
        }
      },
      fail: function (res) {
        console.log(res.errMsg)
      }
    })
  },

  operate: function(e) {
    var that = this;
    var cardId = this.data.cardId;
    var remarkid = e.currentTarget.dataset.remarkid;
    var type = e.currentTarget.dataset.type;
    var state = e.currentTarget.dataset.state;
    var markType = e.currentTarget.dataset.marktype;
    wx.showActionSheet({
      itemList: ['编辑', '删除'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.navigateTo({
            url: '../add-mark/add-mark?cardId=' + cardId + '&remarkid=' + remarkid + '&type=' + type + '&state=' + state + '&markType=' + markType,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '确定删除备注信息？',
            success: function (res) {
              if (res.confirm) {
                wx.request({
                  url: constract.deleteRemark,
                  data: { id: remarkid },
                  success: function(res) {
                    if(res.data.code == 12000) {
                      var cardId = that.data.cardId;
                      var state = that.data.state;
                      app.Verification(function (token) {
                        wx.request({
                          url: constract.remarkList,
                          header: {token, token},
                          data: { cardId: cardId, state: state },
                          success: function (res) {
                            that.setData({
                              remark_list: res.data.data
                            })
                          }
                        })
                      })
                    }
                  }
                })
              } else if (res.cancel) {
                
              }
            }
          })
        }
      },
      fail: function (err) {
        console.log(err.errMsg)
      }
    })
  }
})